package com.inter.entity.interentity.utils;

import com.alibaba.fastjson.JSON;
import com.inter.entity.interentity.home.response.ResponsePackageHome;
import com.inter.entity.interentity.home.response.ResponsePackageHomeData;
import com.inter.entity.interentity.order.response.ResponseOrderPackage;
import com.inter.entity.interentity.order.response.ResponseOrderPackageData;
import com.inter.entity.interentity.queryorder.response.ResponseordersQuery;
import com.inter.entity.interentity.queryorder.response.ResponseordersQueryData;
import com.inter.entity.interentity.travisorder.response.ResponseOrderData;
import com.inter.entity.interentity.travisorder.response.ResponseOrderTradeData;
import com.inter.entity.interentity.travisorder.response.ResponseTravisOrder;
import com.inter.utils.MD5;
import com.inter.utils.Md5Encrypt;

public class UtilsInterfacesPackage {

    /**
     * 计算签名
     *
     * @param str
     * @return
     */
    public String calculateSignature(String str) {
        String sign = Md5Encrypt.md5(str);
        return sign;
    }

    /***
     *签名不通过回复公共报文
     * @param connSeqNo
     * @param tradeTime
     * @param partnerCode
     * @param token
     * @param tradeType
     * @return
     */
    public String publicMessage(String connSeqNo, String tradeTime, String partnerCode, String token, String tradeType) {
        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1111);
        data.setTradeRstMessage("The signature is incorrect, please confirm the signature;签名有误,请确认签名!");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);

        return jsonString;
    }

    public String publicIsopen(String connSeqNo, String tradeTime, String partnerCode, String token, String tradeType) {
        ResponsePackageHomeData opendata = new ResponsePackageHomeData();
        opendata.setConnSeqNo(connSeqNo);
        opendata.setPartnerCode(partnerCode);
        opendata.setToken(token);
        opendata.setTradeType(tradeType);
        opendata.setTradeTime(tradeTime);
        opendata.setTradeRstCode(1112);
        opendata.setTradeRstMessage("The current order is in the open state;当前订单已经是开通状态");
        // 6，给ResponsePackage赋值
        ResponsePackageHome responseOpen = new ResponsePackageHome();
        responseOpen.setData(opendata);
        responseOpen.setSign(MD5.calcMD5(JSON.toJSONString(opendata)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(responseOpen);

        return jsonString;
    }

    //专门用于合作伙伴签名不通过的报文回复

    public String signatureFailMessage(String connSeqNo, String tradeTime, String partnerCode, String token, String deviceId, String deviceType) {
        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1111);
        data.setTradeRstMessage("Signature verification failed;请求签名验证失败");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);

        return jsonString;
    }

    //微信用户用openid查询设备号和设备类型时，openid为null或者为null
    public String openIdIsNull(String connSeqNo, String tradeTime, String partnerCode, String token, String tradeType) {
        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeRstCode(1113);
        data.setTradeRstMessage("Only WeChat users can query;仅微信用户可查询");
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);

        ResponsePackageHome responseiccId = new ResponsePackageHome();
        responseiccId.setData(data);
        responseiccId.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        String jsonString = JSON.toJSONString(responseiccId);
        return jsonString;
    }

    public String FailMessageTokenOrPartnerCode(String connSeqNo, String tradeTime, String partnerCode, String token, String deviceId, String deviceType) {

        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1114);
        data.setTradeRstMessage("Token/partnerCode deviceType is wrong;token/partnerCode/deviceType有误");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;

    }

    public String FailMessageDeviceIdOrDeviceType(String connSeqNo, String tradeTime, String partnerCode, String token, String deviceId, String deviceType) {
        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1115);
        data.setTradeRstMessage("DeviceId or deviceType is illegal;deviceId或者deviceType不合法");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;
    }

    /**
     * 返回请求报文参数不合法问题的方法
     *
     * @param ConnSeqNo
     * @param createTime
     * @return
     */
    public String dataWeiKong(String ConnSeqNo, String createTime) {
        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(ConnSeqNo);
        data.setTradeTime(createTime);
        data.setTradeRstCode(1116);
        data.setTradeRstMessage("Illegal request;请求报文不合法");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;
    }

    public String requestCannotRespondMessage(String connSeqNo, String partnerCode, String tradeTime, String token, String tradeType) {
        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeTime(tradeTime);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeRstCode(1117);
        data.setTradeRstMessage("No response data;请求无响应数据！");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;
    }

    //验证当前token是不是当前渠道的
    public String NoOrderListMessage(String connSeqNo, String partnerCode, String tradeTime, String token, String tradeType) {
        ResponseordersQueryData data = new ResponseordersQueryData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1003);
        data.setTradeRstMessage("The order was not queried;未查询到订单");

        ResponseordersQuery responseQueryorder = new ResponseordersQuery();
        responseQueryorder.setData(data);
        responseQueryorder.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(responseQueryorder);

        return jsonString;
    }

    public String tokenMessage(String connSeqNo, String tradeTime, String partnerCode, String token, String tradeType) {
        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1118);
        data.setTradeRstMessage("Token Validation is wrong;Token验证有误");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);

        return jsonString;
    }

    public String duplicateRequestMessage(String connSeqNo, String partnerCode, String tradeTime, String token, String tradeType) {
        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1119);
        data.setTradeRstMessage("Duplicate request；重复的请求");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        return JSON.toJSONString(resPackages);
    }

    public String errSignature(String connSeqNo, String curtime) {

        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeTime(curtime);
        data.setTradeRstCode(1111);
        data.setTradeRstMessage("The signature is incorrect, please confirm the signature;签名有误,请确认签名!");

        ResponsePackageHome resPackages = new ResponsePackageHome();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        return JSON.toJSONString(resPackages);

    }

    /***
     * 未查询到partnerCode对应的packageCode！
     * 此卡片无权购买此套餐
     * @param connSeqNo
     * @param partnerCode
     * @param tradeTime
     * @param token
     * @param tradeType
     * @return
     */
    public String unableOrder(String connSeqNo, String partnerCode, String tradeTime, String token, String tradeType) {

        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1130);
        data.setTradeRstMessage("No right to order this service, please check；当前企业无权限购买此套餐，请检查！");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        return JSON.toJSONString(resPackages);
    }

    /**
     * @param connSeqNo
     * @param partnerCode
     * @param tradeTime
     * @param token
     * @param tradeType
     * @return
     */
    public String systemAbnormal(String connSeqNo, String partnerCode, String tradeTime, String token, String tradeType) {
        ResponsePackageHomeData data = new ResponsePackageHomeData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(9999);
        data.setTradeRstMessage("Unknown abnormal；未知异常订购失败！");
        // 6，给ResponsePackage赋值
        ResponsePackageHome resPackages = new ResponsePackageHome();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        return JSON.toJSONString(resPackages);
    }

    /***
     * 规范的返回
     * @param tradeRstCode
     * @param tradeRstMessage
     * @param tr
     * @param connSeqNo
     * @param partnerCode
     * @param token
     * @param tradeType
     * @param tradeTime
     * @return
     */
    public String responseStr(int tradeRstCode, String tradeRstMessage, ResponseOrderTradeData tr, String connSeqNo, String partnerCode, String token, String tradeType, String tradeTime) {

        ResponseOrderData data = new ResponseOrderData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(tradeRstCode);
        data.setTradeRstMessage(tradeRstMessage);
        data.setTradeData(tr);

        ResponseTravisOrder response = new ResponseTravisOrder();
        response.setData(data);
        response.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        return JSON.toJSONString(response);
    }

    /**
     * 下单失败
     *
     * @param connNo
     * @param partnerCode
     * @param token
     * @param curTime
     * @param tradeType
     * @return
     */
    public String orderFailed(String connNo, String partnerCode, String token, String curTime, String tradeType) {
        ResponseOrderPackageData data = new ResponseOrderPackageData();
        data.setConnSeqNo(connNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeTime(curTime);
        data.setTradeType(tradeType);
        data.setTradeRstCode(9999);
        data.setTradeRstMessage("unknown error");

        ResponseOrderPackage responseOrder = new ResponseOrderPackage();
        responseOrder.setData(data);
        responseOrder.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        return JSON.toJSONString(responseOrder);
    }

    /**
     * 卡片因为无效所以无法正常下单
     *
     * @param connNo
     * @param partnerCode
     * @param token
     * @param curTime
     * @param tradeType
     * @return
     */
    public String invalidUnableOrder(String connNo, String partnerCode, String token, String curTime, String tradeType) {
        ResponseOrderPackageData data = new ResponseOrderPackageData();
        data.setConnSeqNo(connNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeTime(curTime);
        data.setTradeType(tradeType);
        data.setTradeRstCode(9999);
        data.setTradeRstMessage("The device is invalid and cannot be ordered;设备无效无法订购");

        ResponseOrderPackage responseOrder = new ResponseOrderPackage();
        responseOrder.setData(data);
        responseOrder.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        return JSON.toJSONString(responseOrder);
    }
}
